#pragma once

#include <iostream>
#include <string>
#include "TcpServer.hpp"

using namespace TcpServerModule;
const std::string Sep = "\r\n";
const std::string BlackLine = Sep;
class HttpServer
{
public:
    HttpServer(int port)
        : _tsvr(std::make_unique<TcpServer>(port))
    {
    }
    void Start()
    {
        _tsvr->InitTcpServer([this](SockPtr sockfd, InetAddr client)
                             { return this->HandlerHttpRequest(sockfd, client); });
        _tsvr->Loop();
    }
    // 处理Http的入口
    bool HandlerHttpRequest(SockPtr sockfd, InetAddr client)
    {
        LOG(Loglevel::DEBUG) << "HttpServer: get a new client: " << sockfd->Fd() << " addr info: " << client.Addr();
        // 获取客户端请求
        std::string HttpRequest;
        sockfd->Recv(&HttpRequest);
        std::cout << HttpRequest; // 字节流的请求
        // 1.要保证获取到的是一个完整的报文  TODO
        // 2.反序列化 Response也要进行序列化
        // 状态行
        std::string status_line = "HTTP/1.1 200 OK" + Sep + BlackLine;
        std::string body =
            "<!DOCTYPE html>\
            <html lang=\"zh\">\
            <head>\
            <meta charset = \"UTF-8\">\
            <title> Hello World</title>\
            </head>\
            <body>\
            <h1> Hello World !</h1>\
            </body> </html>";
        std::string Http_Response=status_line+body;
        sockfd->Send(Http_Response);
        return true;
    }
    ~HttpServer()
    {
    }

private:
    std::unique_ptr<TcpServer> _tsvr;
};